require.config({
    baseUrl: './',
    paths: {
        jquery: 'lib/zepto.min',
    	vue: 'lib/vue',
        config: 'js/helper/config',
        comm: 'js/helper/comm',
        route: 'js/helper/route',
        commLight: 'js/app/comm-light'
    },
    shim: {
        jquery: {
            exports: '$'
        }
    }
});

define(['jquery', 'vue', 'config', 'comm', 'route', 'commLight'], function($, vue, _c, comm, route) {

'use strict';
    
    var USER_IDE = comm.getCache(_c.CACHE_USER_IDE);

    comm.tplBottomMenu(vue); // init bottom menu template

    var vm = new vue({
        el: '#vm-main',
        data: {
            init: {
                show: false,
                video: '<img src="images/home/top-video.jpg" />',
                banner: null,
                banner_href: null,
            },
            user: {
                id: comm.getCache('userId'),
                is_manager: Number(comm.getCache(_c.CACHE_USER_DELETE)) === 1,
                is_staff: USER_IDE === _c.USER_IDE_STAFF, // 员工
                is_friend: USER_IDE === _c.USER_IDE_FRIEND, // 亲友
                is_guest: USER_IDE === _c.USER_IDE_GUEST, // 嘉宾
                is_visitor: USER_IDE === _c.USER_IDE_VISITOR // 访客
            },
            add_comment: {
                show: false,
                content: '',
                program_id: ''
            },
            scoring: { // 打分
                id: null,
                show: false,
                ok: true,
                score: '' // 多少分
            },
            program_list: []
        },
        methods: {
            showScoring: function(id) {
                this.scoring.id = id;
                this.scoring.show = true;
                this.scoring.ok = true;
                this.scoring.score = '';
            },
            saveScoring: function() { // 节目评分
                var _this = this,
                    score = this.scoring.score;

                if(score === '' || ! /^[-]{0,1}[0-9]{1,}$/.test(score)) {
                    this.scoring.ok = false;
                    return;
                }

                if(score < 60 || score > 100) {
                    this.scoring.ok = false;
                    return;
                }
                this.scoring.ok = true;

                var params = {score: score, id: this.scoring.id};
                route('/program/scoreProgram.html', {params: params}, function(response, error) {
                    _this.scoring.score = '';
                    _this.scoring.id = null;
                    _this.scoring.show = false;

                    if(! response) {
                        alert(error);
                        return;
                    }
                    
                    app.getProgramList(); // reset list
                });
            },
            toggleComment: function(id) {
                var item = app.getListItem(id);
                if(! item) {
                    return;
                }

                item.hide_comment = ! item.hide_comment;
            },
            addComment: function(program_id) {
                if(program_id) { // show input
                    this.add_comment = {show: true, content: '', program_id: program_id};

                    setTimeout(function() {
                        $('#comment_content').focus();
                    }, 50);
                    return;
                }
                
                // add comment
                if(this.add_comment.content === '') {
                    this.add_comment.program_id = '';
                }
                else {
                    app.addComment();
                }

                this.add_comment.show = false;
            },
            addSupport: function(program_id) {
                if(! program_id) {
                    return;
                }

                app.addSupport(program_id);
            }
        }
    });


    var app = {
        findItemById: function(id) {
            for(var i = 0, len = vm.program_list.length; i < len; i++) {
                if(vm.program_list[i].id === id) {
                    return vm.program_list[i];
                }
            }

            return false;
        },
        setBannerRank: function(status) { // 查看团队排名或颜值排名
            if(status === 3) {
                vm.init.banner = 'images/home/banner-2.jpg';
                vm.init.banner_href = './rank-team.html';
            }
            else {
                vm.init.banner = 'images/home/banner-1.jpg';
                vm.init.banner_href = './rank-team-face.html';
            }
        },
        addComment: function() {
            var _this = this;

            route('/team/addComment.html', {
                params: {
                    id: vm.add_comment.program_id, 
                    content: vm.add_comment.content
                }
            }, function(response, error) {
                if(! response) {
                    alert(error || '发送评论失败');
                    return;
                }

                var item = _this.findItemById(vm.add_comment.program_id);
                if(item) {
                    item.commentList.unshift({id: 0, content: vm.add_comment.content, userName: '我', show: true});
                }

                vm.add_comment.program_id = '';
                vm.add_comment.content = '';
            });
        },
        addSupport: function(program_id) {
            var _this = this;

            route('/user/followTeam.html', {params: {id: program_id}}, function(response, error) {
                if(! response) {
                    alert(error);
                    return;
                }

                // app.getProgramList(); // reset list
                var item = _this.findItemById(program_id);
                if(item) {
                    item.followFlag = 1; // 已点赞
                    item.outScore++;
                }
            });
        },
        getListItem: function(id) {
            for(var i = 0, len = vm.program_list.length; i < len; i++) {
                if(vm.program_list[i].id === id) {
                    return vm.program_list[i];
                }
            }

            return false;
        },
        getProgramList: function() {
            var _this = this;

            route('/program/findProgramList.html', {}, function(response) {
                comm.loading('hide');

                if(! response) {
                    return;
                }

                vm.program_list = _set(response.data.list || []);
            });

            function _set(list) {
                for(var i = 0, len = list.length; i < len; i++) {
                    list[i].no = String(list[i].id).length === 1 ? ('0' + list[i].id) : list[i].id;
                    list[i].pic = (! list[i].pic || list[i].pic === '') ? [] : list[i].pic.split(',');
                    list[i].teamKey = comm.getTeamKey(list[i].teamName);

                    list[i].hide_comment = true;

                    if(list[i].commentList && list[i].commentList.length) {
                        list[i].commentList[0].show = true;
                    }

                    if(i === 0) { // 根据第一个节目，显示团队排名banner
                        _this.setBannerRank(list[i].status);
                    }
                }

                return list;
            }
        },
        init: function() {
            this.getProgramList();
            vm.init.show = true;
        }
    };

    app.init();

});